package com.uroad.jilinpda.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;


import com.uroad.jilinpda.R;
import com.uroad.jilinpda.common.BaseActivity;
import com.uroad.jilinpda.fragment.OBUCardInfoFragment;
import com.uroad.jilinpda.fragment.OBUSystemInfoFragment;
import com.uroad.jilinpda.fragment.OBUVehicleInfoFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @创建者 wlw
 * @创建时间 2019/6/16 16:31
 * @描述
 */
public class OBUQueryInfoActivity extends BaseActivity {
    @BindView(R.id.rgObuInfo)
    RadioGroup rgObuInfo;
    @BindView(R.id.rbSystemInfo)
    RadioButton rbSystemInfo;
    @BindView(R.id.rbCarInfo)
    RadioButton rbCarInfo;
    @BindView(R.id.rbCardInfo)
    RadioButton rbCardInfo;
    @BindView(R.id.vpObuInfo)
    ViewPager vpObuInfo;
    @BindView(R.id.btnConfirm)
    Button btnConfirm;

    private List<Fragment> fragments;

    private String fileSystem = "";
    private String fileVehicle = "";
    private String file0015 = "";
    private String obuNo = "";
    private String cardBalance = "";
    private String randomObu = "";
    private FragmentPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentLayout(R.layout.activity_obu_query_info);
        setTitle("标签检测");
        ButterKnife.bind(this);
        initData();
        initListener();
    }

    private void initData() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            fileSystem = bundle.getString("fileSystem");
            fileVehicle = bundle.getString("fileVehicle");
            file0015 = bundle.getString("file0015");
            obuNo = bundle.getString("obuNo");
            randomObu = bundle.getString("randomObu");
            cardBalance = bundle.getString("cardBalance");
        }
        fragments = new ArrayList<>();
        OBUSystemInfoFragment systemInfoFragment = new OBUSystemInfoFragment();
        systemInfoFragment.setContent(fileSystem);
        fragments.add(systemInfoFragment);

        OBUVehicleInfoFragment vehicleInfoFragment = new OBUVehicleInfoFragment();
        vehicleInfoFragment.setContent(obuNo,fileVehicle,randomObu,fileSystem);
        fragments.add(vehicleInfoFragment);

        OBUCardInfoFragment cardInfoFragment = new OBUCardInfoFragment();
        cardInfoFragment.setContent(file0015, cardBalance);
        fragments.add(cardInfoFragment);

        adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return fragments.size();
            }

            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }
        };
        vpObuInfo.setOffscreenPageLimit(2);
        vpObuInfo.setAdapter(adapter);
    }


    private void initListener() {
        rgObuInfo.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rbSystemInfo:
                        vpObuInfo.setCurrentItem(0);
                        break;
                    case R.id.rbCarInfo:
                        vpObuInfo.setCurrentItem(1);
                        break;
                    case R.id.rbCardInfo:
                        vpObuInfo.setCurrentItem(2);
                        break;
                }
            }
        });

        vpObuInfo.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        rbSystemInfo.setChecked(true);
                        break;
                    case 1:
                        rbCarInfo.setChecked(true);
                        break;
                    case 2:
                        rbCardInfo.setChecked(true);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @OnClick({R.id.btnConfirm})
    public void onClick(View view) {
        finish();
    }

}
